<template>
  <button
    class="lt-button"
    :class="[`lt-button-${type}`, { 'is-active': isActive, 'is-disabled': disabled }, buttonSize]"
    @mousedown="handleMouseDown"
    @mouseup="handleMouseUp"
  >
    <span><slot></slot></span>
  </button>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import type { PropType } from 'vue'

const props = defineProps({
  type: {
    type: String as PropType<'primary' | 'danger' | 'success' | 'warning' | 'info' | 'plain'>,
    default: 'primary'
  },
  disabled: {
    type: Boolean,
    default: false
  },
  size: {
    type: String as PropType<'small' | 'medium' | 'large'>,
    default: 'medium'
  }
})

// 设置按钮的大小
const buttonSize = computed(() => {
  return `lt-button-${props.size}`
})

const isActive = ref<boolean>(false) // 是否按下
const emit = defineEmits(['click'])
// 鼠标按下
const handleMouseDown = (e: MouseEvent) => {
  if (props.disabled) return
  isActive.value = true
  emit('click', e)
}
// 鼠标松开
const handleMouseUp = (e: MouseEvent) => {
  isActive.value = false
}
</script>

<style scoped lang="less">
.lt-button {
  display: inline-block;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  background: #ffffff;
  border: 1px solid #dcdfe6;
  color: #606266;
  text-align: center;
  box-sizing: border-box;
  outline: none;
  margin: 0;
  transition: 0.1s;
  font-weight: 500;
  padding: 12px 20px;
  font-size: 14px;
  user-select: none;
  border-radius: 4px;
  &:hover {
    color: #409eff;
    border-color: #c6e2ff;
    background-color: #ecf5ff;
  }
  &.is-disabled {
    cursor: no-drop;
  }
  &.lt-button-large {
    font-size: 18px;
    width: 100%;
  }
}

.lt-button-plain {
  background: #ffffff;
  border: 1px solid #dcdfe6;
  color: #409eff;
  &:hover {
    background: #ecf5ff;
    border-color: #c6e2ff;
  }
  &.is-active {
    background-color: #eee;
  }
}

.lt-button-danger {
  color: #fff;
  background-color: #f56c6c;
}

.lt-button-primary {
  color: #fff;
  background-color: #409eff;
  border-color: #409eff;
  &:hover {
    background: #66b1ff;
    background-color: #66b1ff;
    color: #fff;
  }
  &.is-active {
    background-color: rgb(51.2, 126.4, 204);
  }
  &.is-plain {
    color: #409eff;
    background: #ecf5ff;
    &:hover {
      background: #409eff;
      border-color: #409eff;
      color: #fff;
    }
  }
}
.lt-button-success {
  color: #fff;
  background-color: #67c23a;
  border-color: #67c23a;
  &:hover {
    background: #85ce61;
    background-color: #85ce61;
    color: #fff;
  }
  &.is-active {
    background-color: rgb(82.4, 155.2, 46.4);
  }
  &.is-plain {
    color: #67c23a;
    background: #c2e7b0;
    &:hover {
      background: #67c23a;
      border-color: #67c23a;
      color: #fff;
    }
  }
}
.lt-button-info {
  color: #fff;
  background-color: #909399;
  border-color: #909399;
  &:hover {
    background: #a6a9ad;
    background-color: #a6a9ad;
    color: #fff;
  }
  &.is-active {
    background-color: rgb(115.2, 117.6, 122.4);
  }
  &.is-plain {
    color: #909399;
    background: #d3d4d6;
    &:hover {
      background: #909399;
      border-color: #909399;
      color: #fff;
    }
  }
}
.lt-button-warning {
  color: #fff;
  background-color: #e6a23c;
  border-color: #e6a23c;
  &:hover {
    background: #ebb563;
    background-color: #ebb563;
    color: #fff;
  }
  &.is-active {
    background-color: rgb(184, 129.6, 48);
  }
  &.is-plain {
    color: #e6a23c;
    background: #f5dab1;
    &:hover {
      background: #e6a23c;
      border-color: #e6a23c;
      color: #fff;
    }
  }
}
.lt-button-danger {
  color: #fff;
  background-color: #f56c6c;
  border-color: #f56c6c;
  &:hover {
    background: #f78989;
    background-color: #f78989;
    color: #fff;
  }
  &.is-active {
    background-color: rgb(196, 86.4, 86.4);
  }
  &.is-plain {
    color: #f56c6c;
    background: #fbc4c4;
    &:hover {
      background: #f56c6c;
      border-color: #f56c6c;
      color: #fff;
    }
  }
}
</style>
